# @Time : 2023/1/6
# @Author : SDAJAVA

import jsonpath
import json

obj = json.load(open("JSONPath使用示例.json", "r", encoding="utf-8"))

# 所有书的作者
author_list = jsonpath.jsonpath(obj, '$.store.book[*].author')
print(author_list)
author_list_1 = jsonpath.jsonpath(obj, '$..author')
print(author_list_1)
# store下所有的元素
tag_list = jsonpath.jsonpath(obj, '$.store.*')
print(tag_list)
# store下所有的东西的price
price_list = jsonpath.jsonpath(obj, '$.store..price')
print(price_list)
# 第三本书
book_no_3 = jsonpath.jsonpath(obj, '$..book[2]')
print(book_no_3)
# 最后一本书
book_last = jsonpath.jsonpath(obj, '$..book[(@.length-1)]')
print(book_last)
# 前两本书
book = jsonpath.jsonpath(obj, '$..book[0,1]')
print(book)
book1 = jsonpath.jsonpath(obj, '$..book[:2]')
print(book1)
# 过滤出所有包含ISBN的书
ISBN_list = jsonpath.jsonpath(obj, '$..book[?(@.isbn)]')
print(ISBN_list)
# 那本书超过10块钱
ten_list = jsonpath.jsonpath(obj, '$..book[?(@.price<10)]')
print(ten_list)
